package com.dlc.shop.huifu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huifu.bspay.sdk.opps.core.request.V2TradePaymentScanpayRefundRequest;
import com.huifu.bspay.sdk.opps.core.utils.DateTools;
import com.huifu.bspay.sdk.opps.core.utils.SequenceTools;
import com.dlc.shop.bean.model.PayInfo;
import com.dlc.shop.bean.pay.RefundInfoDto;
import com.dlc.shop.common.util.IpHelper;
import com.dlc.shop.config.ShopConfig;
import com.dlc.shop.service.PayInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import static com.dlc.shop.huifu.BaseCommonDemo.doExecute;
import static com.dlc.shop.huifu.BaseCommonDemo.doInit;

/**
 * 汇付退款
 */
@Slf4j
@Component
@AllArgsConstructor
public class HuiFuConfig {

    private final ShopConfig shopConfig;
    private final PayInfoService payInfoService;
    /**
     * 申请退款 ..需要回调地址
     */
    public Boolean refundOrder(RefundInfoDto refundInfo) throws Exception {


        doInit(OppsMerchantConfigDemo.getMerchantConfig());
        V2TradePaymentScanpayRefundRequest  request = new V2TradePaymentScanpayRefundRequest();
        // 第三方paypal的交易号
        String captureId = refundInfo.getBizPayNo();
        // payNo 本系统生成的流水号
        String payNo = refundInfo.getPayNo();
        request.setHuifuId("6666000152203117");
        request.setReqDate(DateTools.getCurrentDateYYYYMMDD());
        request.setOrdAmt(refundInfo.getRefundAmount().toString());
        request.setReqSeqId(SequenceTools.getReqSeqId32());

        PayInfo payInfo = payInfoService.getOne(new LambdaQueryWrapper<PayInfo>().eq(PayInfo::getPayNo, payNo));
        //退款时 使用
        //request.setOrgReqDate(payInfo.getReqDate());
        HashMap<String, Object> map = new HashMap<>();
        map.put("org_req_seq_id",captureId);
        //汇付 退款需要回调地址
        map.put("notify_url", "http://y7m9vf.natappfree.cc/huifu/huiFuCallback");
        // 设备信息
        Map<String, String> deviceInfo = new HashMap<>();
        if (IpHelper.getIpAddr() == null) {
            deviceInfo.put("ip_addr", "127.0.0.1");
        } else {
            deviceInfo.put("ip_addr", IpHelper.getIpAddr());
        }
        map.put("risk_check_data", JSON.toJSONString(deviceInfo));
        request.setExtendInfo(map);

        try {
            // 3. 发起API调用
            Map<String, Object> response = doExecute(request);
            System.out.println("返回数据:" + JSONObject.toJSONString(response));
            String subRespCode = (String )response.get("sub_resp_code");
            if( subRespCode.equals("00000000")){
                if(response.get("trans_stat").equals("P")){
                    return false;
                }
                if(response.get("trans_stat").equals("S")){
                    return true;
                }
                if(response.get("trans_stat").equals("F")){
                    return false;
                }
                return false;
            }else{


                System.err.println("交易失败，业务返回码="+subRespCode+",业务返回描述="+(String )response.get("sub_resp_desc"));
           return false;
            }

        } catch (Exception e) {
            log.info("退款失败："+e.getMessage());
            return false;
        }
    }
}
